import Vue from 'vue'
import VueRouter from 'vue-router'

//需要导入的组件
import Login from '../views/login/Login.vue'
import Layout from '../views/layout/Layout.vue'


//重写VueRouter原型上push方法,解决自己跳转自己出错误的一个bug
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)
const routes = [
  //登录
  {
    path: '/login',
    component: Login
  },
  //后台首页
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    meta: { path: '/home', title: '后台首页' },
    children: [
      {
        path: '/home',
        component: () => import('../views/home/Home.vue')
      }
    ]
  },
  //订单管理
  {
    path: '/order',
    component: Layout,
    meta: { path: '/order', title: '订单管理' },
    children: [
      {
        path: '/order',
        component: () => import('../views/order/Order.vue')
      }
    ]
  },
  //账号管理
  {
    path: '/account',
    component: Layout,
    redirect: '/account/acclist',
    meta: { path: '/account', title: '账号管理' },
    children: [
      {
        path: '/account/acclist',
        meta: { path: '/account/acclist', title: '账号列表' },
        component: () => import('../views/account/AccountList.vue')
      },
      {
        path: '/account/addacc',
        meta: { path: '/account/addacc', title: '添加账号' },
        component: () => import('../views/account/AddAcc.vue')
      },
      {
        path: '/account/password',
        meta: { path: '/account/password', title: '修改密码' },
        component: () => import('../views/account/Password.vue')
      },
      {
        path: '/account/personal',
        meta: { path: '/account/personal', title: '个人中心' },
        component: () => import('../views/account/Personal.vue')
      },
    ]
  },
  //商品管理
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/addgoods',
    meta: { path: '/goods', title: '商品管理' },
    children: [
      {
        path: '/goods/addgoods',
        meta: { path: '/goods/addgoods', title: '添加商品' },
        component: () => import('../views/goods/AddGoods.vue')
      },
      {
        path: '/goods/goodscate',
        meta: { path: '/goods/goodscate', title: '商品分类' },
        component: () => import('../views/goods/GoodsCate.vue')
      },
      {
        path: '/goods/goodslist',
        meta: { path: '/goods/goodslist', title: '商品列表' },
        component: () => import('../views/goods/GoodsList.vue')
      },
    ]
  },
  //店铺管理
  {
    path: 'shop',
    component: Layout,
    meta: { path: '/shop', title: '店铺管理' },
    children: [
      {
        path: '/shop',
        component: () => import('../views/shop/Shop.vue')
      }
    ]
  },
  //统计
  {
    path: '/total',
    component: Layout,
    redirect: '/total/goodstotal',
    meta: { path: '/total', title: '销售统计' },
    children: [
      {
        path: '/total/goodstotal',
        component: () => import('../views/total/GoodsTotal.vue'),
        meta: { path: '/total/goodstotal', title: '商品统计' }
      },
      {
        path: '/total/ordertotal',
        component: () => import('../views/total/OrderTotal.vue'),
        meta: { path: '/total/ordertotal', title: '订单统计' }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

//设置全局守卫
router.beforeEach((to,from, next) => {
  let token = localStorage.getItem('tk') ? true : false;
  if (token) {
    next()
  } else {
    // window.location.href = "#/login"
    if(to.path == "/login"){
      next()
    }else{
      alert('请先登录')
      next('/login')
      localStorage.clear()
    }
  }
})


//暴露路由对象
export default router


